<?php

namespace App\Admin\Actions\Grid;

use Dcat\Admin\Actions\Response;
use Dcat\Admin\Grid\RowAction;
use Dcat\Admin\Traits\HasPermissions;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Models\CustomerInfo;
use Illuminate\Support\Carbon;

class ConfirmBuy extends RowAction
{
    /**
     * @return string
     */
    protected $title = '确认购车';

    /**
     * Handle the action request.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function handle(Request $request)
    {
        $id = $this->getKey();
        $clue = CustomerInfo::findOrFail($id);
        if ($clue->buy_car_at) {
            return $this->response()->error('此记录已确认过了！');
        }
        $clue->buy_car_at = Carbon::now();
        $clue->save();

        return $this->response()
            ->success('操作成功')
            ->refresh();
    }

    /**
     * @return string|array|void
     */
    public function confirm()
    {
        return ['是否确认购车?', ''];
    }

    /**
     * @param Model|Authenticatable|HasPermissions|null $user
     *
     * @return bool
     */
    protected function authorize($user): bool
    {
        return true;
    }

    /**
     * @return array
     */
    protected function parameters()
    {
        return [];
    }
}
